package com.tadpole.music.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.wechat.IwxView;
import com.tadpole.music.presenter.wechat.OpenIdPresenter;
import com.tadpole.music.utils.ToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static com.tadpole.music.app.MyApplication.mWxApi;


/**
 * 微信登录返回的界面
 */
public class WXEntryActivity extends Activity  implements IWXAPIEventHandler, IwxView {
    private OpenIdPresenter openIdPresenter;
    private String flag;
    //微信请求响应
    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWxApi = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID, true);
        mWxApi.handleIntent(this.getIntent(), this);
    }

    //发送到微信请求的响应结果
    @Override
    public void onResp(BaseResp resp) {

        switch (resp.errCode) {

            case BaseResp.ErrCode.ERR_OK:
                //发送成功
                SendAuth.Resp sendResp = (SendAuth.Resp) resp;
                if (sendResp != null) {
                    String code = sendResp.code;
                    flag = sendResp.state;
                    openIdPresenter = new OpenIdPresenter();
                    openIdPresenter.attachView(this);
                    openIdPresenter.getOpenId(code);
                    WXEntryActivity.this.finish();
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                ToastUtils.show("已取消");
                WXEntryActivity.this.finish();
                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                ToastUtils.show("已拒绝");
                WXEntryActivity.this.finish();
                //发送被拒绝
                break;
            default:
                //发送返回
                WXEntryActivity.this.finish();
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);

        mWxApi.handleIntent(intent, this);
        WXEntryActivity.this.finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        WXEntryActivity.this.finish();
    }


    @Override
    public void showOpenId(String openId,String ac) {
        //可以发个广播试试看看
        Intent intent = new Intent();

        if(flag.equals("1")){
            intent.setAction(Constant.BIND_WX_GET_OPENID);
        }else {
            intent.setAction(Constant.BIND_WX_GET_OPENID11111);
        }

        Bundle bundle = new Bundle();
        bundle.putString(Constant.KEY_WX_OPENID, openId);
        bundle.putString(Constant.ACCESS_TOKEN,ac);
        intent.putExtras(bundle);
        sendBroadcast(intent);
        WXEntryActivity.this.finish();

    }

    @Override
    public void showError() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showErr() {

    }

    @Override
    public Context getContext() {
        return null;
    }

}
